/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package descriptor;

import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author Alejandro
 */
public final class Application {

    public static final String PATH = "application.xml";
    private static volatile Application instance;
    private static Map<String, SortingStyle> sortingMap;
    private static String title;
    private static String connectionString;
    private static Iterable<Template> templateCollection;


    private Application() {
        templateCollection = new LinkedList<Template>();
        sortingMap = new TreeMap<String, SortingStyle>();
    }

    public static Application getInstance() {
        if (instance == null) {
            synchronized (Application.class) {
                if (instance == null) {
                    instance = new Application();
                    Initializer in=new XmlParser(PATH);
                    in.setManagedObject(ManagedObject.Application);
                    Map<String,Object> map = (Map<String,Object>)in.init(instance);
                    title = (String) map.get("title");
                    connectionString = (String) map.get("connectionString");
                    templateCollection=(Iterable<Template>) map.get("templates");
                }
            }
        }
        return instance;
    }

    public String getTitle() {
        return title;
    }

    public Template getTemplate(String templateAlias) {
        Template result = null;
        for (Template t: templateCollection) {
            if (templateAlias.equals(t.getAlias())) {
                result = t;
                break;
            }
        }
        return result;
    }

    public Iterable<Template> getTemplateCollection(){
        return templateCollection;
    }

    public String getConnectionString() {
        return connectionString;
    }
}
